using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
        private List<SKYFILE> MapSKYFILE(XElement item2, bool mergedb)
        {
            List<SKYFILE> result = new List<SKYFILE>();
            var item4 = from item3 in item2.Elements()
                        select new SKYFILE
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            STBNAME = (string)item3.Element("STBNAME"),
                            SFILENAME = (string)item3.Element("SFILENAME"),
                            SFILEID = (string)item3.Element("SFILEID"),
                            SLOCAL = (string)item3.Element("SLOCAL"),
                            DOWNLOADED = (string)item3.Element("DOWNLOADED"),
                            SINDB = (string)item3.Element("SINDB"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          if (mergedb)
            {                  
               var rowsexist = from u in LocalDB.SKYFILE.ToList().AsQueryable()
                                           join uc in result.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.SKYFILE.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.SKYFILE.InsertAllOnSubmit(result);
                LocalDB.SubmitChanges();
            }
        
            return result;
        }
    }
    [Table(Name = "SKYFILE")]
    public class SKYFILE : INotifyPropertyChanged,INotifyPropertyChanging
    {
        private string _MAINKEY="0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    NotifyPropertyChanging("MAINKEY");
                     _MAINKEY = value;
                     NotifyPropertyChanged("MAINKEY");
                }
            }
        }
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _STBNAME="0";
        [Column]
        public string STBNAME
        {
            get
            {
                return _STBNAME;
            }
            set
            {
                if (value != _STBNAME)
                {
                    NotifyPropertyChanging("STBNAME");
                     _STBNAME = value;
                    NotifyPropertyChanged("STBNAME");
                }
            }
        }
        private string  _SFILENAME="0";
        [Column]
        public string SFILENAME
        {
            get
            {
                return _SFILENAME;
            }
            set
            {
                if (value != _SFILENAME)
                {
                    NotifyPropertyChanging("SFILENAME");
                     _SFILENAME = value;
                    NotifyPropertyChanged("SFILENAME");
                }
            }
        }
        private string  _SFILEID="0";
        [Column]
        public string SFILEID
        {
            get
            {
                return _SFILEID;
            }
            set
            {
                if (value != _SFILEID)
                {
                    NotifyPropertyChanging("SFILEID");
                     _SFILEID = value;
                    NotifyPropertyChanged("SFILEID");
                }
            }
        }
        private string  _SLOCAL="0";
        [Column]
        public string SLOCAL
        {
            get
            {
                return _SLOCAL;
            }
            set
            {
                if (value != _SLOCAL)
                {
                    NotifyPropertyChanging("SLOCAL");
                     _SLOCAL = value;
                    NotifyPropertyChanged("SLOCAL");
                }
            }
        }
        private string  _DOWNLOADED="0";
        [Column]
        public string DOWNLOADED
        {
            get
            {
                return _DOWNLOADED;
            }
            set
            {
                if (value != _DOWNLOADED)
                {
                    NotifyPropertyChanging("DOWNLOADED");
                     _DOWNLOADED = value;
                    NotifyPropertyChanged("DOWNLOADED");
                }
            }
        }
        private string  _SINDB="0";
        [Column]
        public string SINDB
        {
            get
            {
                return _SINDB;
            }
            set
            {
                if (value != _SINDB)
                {
                    NotifyPropertyChanging("SINDB");
                     _SINDB = value;
                    NotifyPropertyChanged("SINDB");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<SKYFILE> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\SKYFILE\\SKYFILE" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\SKYFILE" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("SKYFILE");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (SKYFILE row in ListInput)
                {
                    XElement f = new XElement("SKYFILE");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement STBNAME = new XElement("STBNAME");
                         STBNAME.Value = row.STBNAME;
                          f.Add(STBNAME);
                        XElement SFILENAME = new XElement("SFILENAME");
                         SFILENAME.Value = row.SFILENAME;
                          f.Add(SFILENAME);
                        XElement SFILEID = new XElement("SFILEID");
                         SFILEID.Value = row.SFILEID;
                          f.Add(SFILEID);
                        XElement SLOCAL = new XElement("SLOCAL");
                         SLOCAL.Value = row.SLOCAL;
                          f.Add(SLOCAL);
                        XElement DOWNLOADED = new XElement("DOWNLOADED");
                         DOWNLOADED.Value = row.DOWNLOADED;
                          f.Add(DOWNLOADED);
                        XElement SINDB = new XElement("SINDB");
                         SINDB.Value = row.SINDB;
                          f.Add(SINDB);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
        public bool IsFirst=false;
        private string _PHOTO = "YES";
        public string PHOTO
        {
            get
            {
                return _PHOTO;
            }
            set
            {
                if (value != _PHOTO)
                {
                    NotifyPropertyChanging("PHOTO");
                    _PHOTO = value;
                    NotifyPropertyChanged("PHOTO");
                    NotifyPropertyChanged("PHOTOISOSource");
                }
            }
        }
        public string PHOTOSource
        {
            get
            {
                string image = string.Format("/Data/Images/{0}", "YES");
                if (!PHOTO.Contains("http://"))
                {
                    return string.Format("/Data/Images/{0}", PHOTO);
                }
                return PHOTO;
            }
        }
    public BitmapImage PHOTOISOSource
        {
            get
            {
                string image = string.Format("Data/PhoneDB/Pictures/{0}.jpg",PHOTO);
                BitmapImage bi = new BitmapImage();
                IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
                if(!iso.FileExists(image))
                    image = string.Format("Data/PhoneDB/Pictures/{0}.jpg", "YES");
                using (IsolatedStorageFileStream fileStream = iso.OpenFile(image, FileMode.Open, FileAccess.Read))
                {
                    //bi.SetSource(fileStream);
                }              
                return bi;
            }
        }                                        
    }
}
